package com.cp.sms.service.impl;

import com.cp.common.exception.Assert;
import com.cp.common.exception.BusinessException;
import com.cp.common.result.ResponseEnum;
import com.cp.sms.feign.CoreUserInfoFeign;
import com.cp.sms.service.SmsService;
import com.cp.sms.util.RandomUtils;
import com.cp.sms.util.RegexValidateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author ming
 * @create 2022/7/29
 * @description:
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Resource
    CoreUserInfoFeign coreUserInfoFeign;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public void send(String mobile) {
        //验证
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.ERROR);
        //TODO 验证手机号是否被注册过

        Assert.isTrue(!coreUserInfoFeign.checkMobile(mobile),ResponseEnum.MOBILE_EXIST_ERROR);
        //生成验证码
        HashMap<String, Object> map = new HashMap<>();
        String generateCode = RandomUtils.getFourBitRandom();
        map.put("code",generateCode);

        ////发送验证码
        //DefaultProfile profile = DefaultProfile.getProfile(
        //        SmsProperties.REGION_ID,
        //        SmsProperties.KEY_ID,
        //        SmsProperties.KEY_SECRET
        //);
        //DefaultAcsClient client = new DefaultAcsClient(profile);
        //CommonRequest request = new CommonRequest();
        //request.setSysMethod(MethodType.POST);
        //request.setSysDomain("dysmsapi.aliyuncs.com");
        //request.setSysVersion("2017-05-25");
        //request.setSysAction("SendSms");
        //request.putQueryParameter("RegionId",SmsProperties.REGION_ID);
        //request.putQueryParameter("PhoneNumbers",mobile);
        //request.putQueryParameter("SignName",SmsProperties.SIGN_NAME);
        //request.putQueryParameter("TemplateCode", SmsProperties.TEMPLATE_CODE);
        //Gson gson = new Gson();
        //String s = gson.toJson(map);
        //request.putQueryParameter("TemplateParam",s);
        try {
            //CommonResponse response = client.getCommonResponse(request);
            ////通信失败
            //boolean success = response.getHttpResponse().isSuccess();
            //Assert.isTrue(success,ResponseEnum.ALIYUN_RESPONSE_ERROR);
            //String data = response.getData();
            //log.info("阿里云返回结果{}",data);
            //HashMap resultMap = gson.fromJson(data, HashMap.class);
            //String  dataCode  = (String) resultMap.get("Code");
            ////业务失败
            //Assert.notEquals("isv.BUSINESS_LIMIT_CONTROL",dataCode,ResponseEnum.ALIYUN_SMS_LIMIT_CONTROL_ERROR);
            //Assert.equals("OK",dataCode,ResponseEnum.ALIYUN_SMS_ERROR);
            //验证码存入redis
            redisTemplate.opsForValue().set("srb:sms:code:"+mobile,generateCode,5, TimeUnit.MINUTES);
        } catch (Exception e) {
        //  log.error("阿里云发送调用sdk失败{}",e.getErrCode()+","+e.getErrMsg());
          throw new BusinessException(ResponseEnum.ALIYUN_SMS_ERROR,e);

        }
    }
}
